package fr.cie.blog.modeldao.hib;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import fr.cie.blog.beans.Card;
import fr.cie.blog.beans.User;
import fr.cie.blog.domainservice.IServiceCard;

/**
 * @author AetM
 * 
 */
public class CardDaoImplHib extends HibernateDaoSupport implements
		IServiceCard {

	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger.getLogger(CardDaoImplHib.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#save(java.lang.Object)
	 */
	@Override
	public final int save(final Card card) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(card);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Card error with card.id=" + card.getId()
					+ ", card.address=" + card.getAddress(), e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#update(java.lang.Object)
	 */
	@Override
	public final int update(final Card card) {
		// doesn't work
		// getHibernateTemplate().saveOrUpdate(card);

		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(card);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Card error with card.name=" + card.getName(), e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#delete(java.lang.Object)
	 */
	@Override
	public final int delete(final Card card) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.delete(card);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Card error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findAll()
	 */
	@Override
	public final List<Card> findAll() {
		@SuppressWarnings("unchecked")
		final List<Card> lcard = getHibernateTemplate()
				.find("From Card as c");
		LOG.fatal("lcard size" + lcard.size());
		return lcard;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findById(int)
	 */
	@Override
	public final Card findById(final long pIdentifier) {

		LOG.fatal("pIdentifier ?" + pIdentifier);
		final Card card = getHibernateTemplate().get(Card.class, pIdentifier);
		LOG.fatal("cd ?? ?" + card.getName());

		return card;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.cie.blog.domainservice.IServiceCard#findByUser(fr.cie.blog.beans.User)
	 */
	@Override
	public final Card findByUser(final User pUser) {
		final Card user = (Card) getHibernateTemplate().find(
				"from Card as c where c.owner.id = ?", pUser.getId()).get(0);
		return user;
	}

}